package cn.mw.service.impl;

import cn.mw.domain.Build;
import cn.mw.domain.Device;
import cn.mw.domain.Floor;
import cn.mw.feign.DeviceFeign;
import cn.mw.feign.FloorFeign;
import cn.mw.mapper.BuildMapper;
import cn.mw.service.IBuildService;
import cn.mw.vo.BuildVo;
import cn.mw.vo.DeviceList;
import cn.mw.vo.FloorList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class BuildServiceImpl implements IBuildService {
    @Autowired
    private BuildMapper buildMapper;
    @Autowired
    private FloorFeign feign;
    @Autowired
    private DeviceFeign deviceFeign;

    @Override
    public BuildVo getDeviceListByBuildId(Long id) {
        //通过id查询楼栋
         Build build =  buildMapper.getById(id);
        ArrayList<FloorList> floorList = new ArrayList<>();
        //调用feign接口查询楼栋的所有楼层的集合
        List<Floor> getfloor = feign.getfloor(String.valueOf(build.getId()));
        //集合判空
        if (!CollectionUtils.isEmpty(getfloor)){
            for (Floor floor : getfloor) {
                List<DeviceList> deviceLists = new ArrayList<>();
                //调用feign接口查询楼层下的所有设备
                List<Device> device = deviceFeign.getDevice(String.valueOf(floor.getId()));
                for (Device device1 : device) {
                    DeviceList deviceList = new DeviceList();
                    if (device1.getType() == 1) {
                        deviceList.setTybe("烟感");
                    } else if (device1.getType() == 2) {
                        deviceList.setTybe("手报");
                    } else if (device1.getType() == 3) {
                        deviceList.setTybe("摄像头");
                    } else if (device1.getType() == 4) {
                        deviceList.setTybe("冲锋");
                    }
                    DeviceList deviceList1 = DeviceList.builder()
                            .tybe(deviceList.getTybe())
                            .name(device1.getName())
                            .build();
                    deviceLists.add(deviceList1);
                    }
                FloorList floorList1 = FloorList.builder()
                        .name(floor.getName())
                        .deviceLists(deviceLists)
                        .build();
                floorList.add(floorList1);
            }
        }
        BuildVo buildVo = BuildVo.builder()
                .buildName(build.getBuildName())
                .floorListList(floorList)
                .build();
        return buildVo;
    }
}
